package com.zone.vehicle.superApps.view.home.app.main;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.zlylib.horizontalviewlib.HorizontalView;
import com.zlylib.horizontalviewlib.InfiniteScrollAdapter;
import com.zone.sdk.module.appstore.base.AppStoreResult;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.vehicle.superApps.databinding.FragmentAppHorizontalBinding;
import com.zone.vehicle.superApps.model.HomeViewModel;
import com.zone.vehicle.superApps.model.ViewModelFactory;
import com.zone.vehicle.superApps.view.common.HandledFragment;
import com.zone.vehicle.superApps.view.common.listener.ActionEnum;
import com.zone.vehicle.superApps.view.common.listener.ActionListener;
import com.zone.vehicle.superApps.view.home.app.adapter.HomeAppAdapterAdapter;

import static com.zone.vehicle.superApps.view.home.HomeFragment.APP_DETAIL_FRAGMENT;

public class AppHorizontalFragment extends HandledFragment {

    private static final String TAG = AppHorizontalFragment.class.getSimpleName();

    private FragmentAppHorizontalBinding binding;

    private InfiniteScrollAdapter infiniteAdapter;

    private HomeViewModel homeViewModel;

    private HomeAppAdapterAdapter homeAppAdapterAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentAppHorizontalBinding.inflate(inflater);
        init();
        return binding.getRoot();
    }

    private void init(){
        homeViewModel = new ViewModelProvider(requireActivity(), new ViewModelFactory()).get(HomeViewModel.class);
        homeAppAdapterAdapter = new HomeAppAdapterAdapter(null, new AppItemOnClickListener());
        infiniteAdapter = InfiniteScrollAdapter.wrap(homeAppAdapterAdapter);
        binding.itemPicker.setAdapter(infiniteAdapter);
        binding.itemPicker.addOnItemChangedListener(new HorizontalView.OnItemChangedListener<RecyclerView.ViewHolder>() {
            @Override
            public void onCurrentItemChanged(@Nullable RecyclerView.ViewHolder viewHolder, int position) {
//                int positionInDataSet = infiniteAdapter.getRealPosition(position);
//                onItemChanged(CardBean.getData().get(positionInDataSet));
                Log.d(TAG, "onCurrentItemChanged position="+position);
//                ApiManager.appList(new AppListRequest(), );
            }
        });

        addListener(new ActionListener() {
            @Override
            public boolean onAction(ActionEnum action) {
//                if (action == ACTION_DETAIL_BACK){
//                    changePage(HOME_APP_FRAGMENT);
//                    return true;
//                }
                return false;
            }

            @Override
            public boolean onPageChanged(String pageName) {
                if (pageName.equals("AppHorizontalFragment")){
                    homeViewModel.getHomeAppListData();
                    return true;
                }
                return false;
            }
        });

        homeViewModel.getHomeAppList().observe(getViewLifecycleOwner(), new Observer<AppStoreResult<AppEntity>>() {
            @Override
            public void onChanged(AppStoreResult<AppEntity> appEntities) {
                homeAppAdapterAdapter.setData(appEntities);
                homeAppAdapterAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        homeViewModel.getHomeAppListData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }

    public class AppItemOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            AppEntity app = (AppEntity)v.getTag();
            Log.d(TAG, "app:"+app.getName());
            homeViewModel.setCurrentApp(app);
//            onPageChanged(APP_DETAIL_FRAGMENT);
            changePage(APP_DETAIL_FRAGMENT);
        }
    }
}
